package site.yunnong.atvris.portal.service.impl;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import site.yunnong.atvris.common.api.Page;
import site.yunnong.atvris.common.api.ResultCodeEnum;
import site.yunnong.atvris.common.dto.CommentQueryParam;
import site.yunnong.atvris.common.dto.VideoQueryParam;
import site.yunnong.atvris.common.enumeration.MediaTypeEnum;
import site.yunnong.atvris.common.enumeration.RankTypeEnum;
import site.yunnong.atvris.common.exception.YunnongBusinessException;
import site.yunnong.atvris.dao.domain.Video;
import site.yunnong.atvris.dao.domain.VideoProfile;
import site.yunnong.atvris.portal.service.CommentsService;
import site.yunnong.atvris.portal.service.FrontTagsService;
import site.yunnong.atvris.portal.service.FrontVideoService;
import site.yunnong.atvris.portal.service.UserManageService;
import site.yunnong.atvris.service.InteractionService;
import site.yunnong.atvris.service.UserStarService;
import site.yunnong.atvris.service.VideoProfileService;
import site.yunnong.atvris.service.VideoService;
import site.yunnong.atvris.service.helper.BuildHelper;
import site.yunnong.atvris.service.helper.OperatorHelper;
import site.yunnong.atvris.service.vo.PlayerPageVO;
import site.yunnong.atvris.service.vo.VideoCardVO;
import site.yunnong.atvris.service.vo.VideoDetailsVO;
import site.yunnong.atvris.service.vo.VideoTagsVO;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zjh
 * @date 2021/9/7 12:07
 */
@Slf4j
@Service
public class FrontVideoServiceImpl implements FrontVideoService {
    
    @Resource
    private VideoService videoService;
    @Resource
    private VideoProfileService videoProfileService;
    @Resource
    private UserStarService userStarService;
    @Resource
    private InteractionService interactionService;
    @Resource
    private UserManageService userManageService;
    @Resource
    private CommentsService commentsService;
    @Resource
    private FrontTagsService frontTagsService;
    
    @Override
    public VideoDetailsVO queryVideoInfoByUvid(Long uvid) {
        Video video = videoService.queryApprovedVideoByUvid(uvid);
        if (video == null) {
            throw new YunnongBusinessException(ResultCodeEnum.ERROR_BUSINESS, "无此视频");
        }
        VideoProfile videoProfile = videoProfileService.queryByUvid(uvid);
        VideoDetailsVO vo = BuildHelper.buildVideoDetailsVO(video, videoProfile);
        // tags
        List<VideoTagsVO> videoTagsVOList = frontTagsService.queryVideoTags(uvid);
        vo.setVideoTagList(videoTagsVOList);
        if (OperatorHelper.isLogin()) {
            Long uuid = OperatorHelper.getLoginUuid();
            // 是否点赞
            boolean mediaLikeStatus = interactionService.findMediaLikeStatus(uuid, MediaTypeEnum.VIDEO.getValue(), uvid);
            vo.setHasSupport(mediaLikeStatus);
            // 是否收藏
            int mediaStarStatus = userStarService.findMediaStarStatus(uuid, MediaTypeEnum.VIDEO.getValue(), uvid);
            vo.setHasStar(mediaStarStatus);
        } else {
            vo.setHasSupport(false);
            vo.setHasStar(0);
        }
        // 播放量
        int mediaViewCount = interactionService.findMediaViewCount(MediaTypeEnum.VIDEO.getValue(), uvid);
        vo.setViewCount(mediaViewCount);
        // 点赞量
        int likedCount = interactionService.findMediaLikedCount(MediaTypeEnum.VIDEO.getValue(), uvid);
        vo.setLikedCount(likedCount);
        return vo;
    }

    @Override
    public List<VideoCardVO> queryRecommendListByUvid(Long uvid, Integer pageSize) {
        Video video = videoService.queryApprovedVideoByUvid(uvid);
        if (video == null) {
            log.error("FrontVideoServiceImpl.queryRecommendListByUvid, uvid: {}, 未查到相关视频数据", uvid);
            throw new YunnongBusinessException(ResultCodeEnum.ERROR_BUSINESS, "当前视频异常");
        }
        
        List<Video> videoList = videoService.selectRandomVideoList(pageSize, video.getPid());
        
        return Optional.of(videoList).orElse(Lists.newArrayList()).stream().map(item -> {
            VideoCardVO vo = new VideoCardVO();
            BeanUtils.copyProperties(item, vo);
            vo.setLikedCount(interactionService.findMediaLikedCount(MediaTypeEnum.VIDEO.getValue(), item.getUvid()));
            vo.setViewCount(interactionService.findMediaViewCount(MediaTypeEnum.VIDEO.getValue(), item.getUvid()));
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public PlayerPageVO queryPlayerPageInfo(Long uvid) {
        PlayerPageVO vo = new PlayerPageVO();
        VideoDetailsVO videoDetailsVO = queryVideoInfoByUvid(uvid);
        vo.setVideoInfo(videoDetailsVO);
        vo.setUserInfo(userManageService.queryInfoByUuid(videoDetailsVO.getUuid()));
        CommentQueryParam commentQueryParam = CommentQueryParam.builder()
                .mediaId(uvid)
                .mediaType(MediaTypeEnum.VIDEO.getValue())
                .pageNum(1)
                .pageSize(10)
                .sortType(RankTypeEnum.HOT_DEGREE.getValue())
                .sortRule("desc")
                .build();
        vo.setCommentPageVO(commentsService.listWithReply(commentQueryParam));
        return vo;
    }

    @Override
    public Page<VideoCardVO> queryUserVideoPage(VideoQueryParam param) {
        return videoService.queryUserVideoPage(param);
    }
}
